package com.zhijian.medical.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.zhijian.medical.dao.local.RoutinePatientYzDetailMapper;
import com.zhijian.medical.dao.local.RoutinePatientYzMapper;
import com.zhijian.medical.entity.po.RoutinePatient;
import com.zhijian.medical.entity.po.RoutinePatientYz;
import com.zhijian.medical.entity.po.RoutinePatientYzDetail;
import com.zhijian.medical.entity.vo.request.RoutinePatientYzSearchVo;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.util.page.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class RoutinePatientYzService {

    @Autowired
    RoutinePatientYzMapper routinePatientYzMapper;

    @Autowired
    RoutinePatientYzDetailMapper routinePatientYzDetailMapper;

    public List<RoutinePatientYz> selectYzDetailPageList(RoutinePatientYzSearchVo searchVo){
        return routinePatientYzMapper.selectYzDetailPageList(searchVo);
    }

    public Page<RoutinePatientYz> selectPageList(RoutinePatientYzSearchVo searchVo){
        handleReqParam(searchVo);
        PageHelper.startPage(searchVo.getPageNum(), searchVo.getPageSize());
        com.github.pagehelper.Page<RoutinePatientYz> routinePatientYzPage = routinePatientYzMapper.selectPageList(searchVo);
        List<RoutinePatientYz> dataList = routinePatientYzPage.getResult();
//        List<Long> yzIdList = dataList.stream().map(RoutinePatientYz::getId).collect(Collectors.toList());
//        if (CollectionUtil.isNotEmpty(yzIdList)){
//            Map<Long, Integer> result = groupYzData(yzIdList);
//            for (RoutinePatientYz routinePatientYz : dataList){
//                Integer count = result.get(routinePatientYz.getId());
//                if (count == null){
//                    routinePatientYz.setTakeNumber("0");
//                }else {
//                    routinePatientYz.setTakeNumber(count.toString());
//                }
//            }
//        }
        Page<RoutinePatientYz> result =
                new com.zhijian.util.page.Page<>(true,
                        Integer.parseInt( String.valueOf(routinePatientYzPage.getTotal())), searchVo.getPageNum(), searchVo.getPageSize());
        result.setData(dataList);
        return result;
    }

    private Map<Long, Integer> groupYzData(List<Long> yzIdList){
        QueryWrapper<RoutinePatientYzDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("yz_id", yzIdList);
        List<RoutinePatientYzDetail> dataList = routinePatientYzDetailMapper.selectList(queryWrapper);
        Map<Long, List<RoutinePatientYzDetail>> result = dataList.stream().collect(Collectors.groupingBy(RoutinePatientYzDetail::getYzId));
        Map<Long, Integer> rtnResult = new HashMap<>();
        for (Map.Entry<Long, List<RoutinePatientYzDetail>> entry : result.entrySet()){
            rtnResult.put(entry.getKey(), entry.getValue().size());
        }
        return rtnResult;
    }

    private void handleReqParam(RoutinePatientYzSearchVo searchVo){
        String blhQuery = searchVo.getConventionPathologyNumber();
        String numberQuery = searchVo.getNumber();
        if (StringUtils.hasText(blhQuery)){
            if (blhQuery.contains("~")){
                String[] val = blhQuery.split("~");
                Set<String> blhList = new LinkedHashSet<>();
                AssertUtil.check(val.length != 2, "病理号查询条件异常");
                String prefix = val[0].substring(0, val[0].length() - 5);
                int startIndex = Integer.parseInt(val[0].substring(val[0].length() - 5));
                int endIndex = Integer.parseInt(val[1]);
                int i = 0;
                for ( ;startIndex <= endIndex; startIndex++){
                    String code = org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(startIndex), 5 ,"0");
                    blhList.add(prefix + code);
                    i++;
                    if (i>100){
                        break;
                    }
                }
                searchVo.setBlhList(new LinkedList<>(blhList));
            }else {
                List<String> blhList = new LinkedList<>();
                blhList.add(blhQuery);
                searchVo.setBlhList(blhList);
            }
        }
        if (StringUtils.hasText(numberQuery)){
            if (numberQuery.contains("~")){
                String[] val = numberQuery.split("~");
                Set<String> blhList = new LinkedHashSet<>();
                AssertUtil.check(val.length != 2, "病理号查询条件异常");
                String prefix = val[0];
                String newPrefix = prefix.substring(0, prefix.lastIndexOf("-"));
                int startIndex = Integer.parseInt(prefix.substring(prefix.lastIndexOf("-") + 1));
                int endIndex = Integer.parseInt(val[1]);
                int i = 0;
                for ( ;startIndex <= endIndex; startIndex++){
                    blhList.add(newPrefix + "-" + startIndex);
                    i++;
                    if (i>100){
                        break;
                    }
                }
                searchVo.setLkhList(new LinkedList<>(blhList));
            }else {
                List<String> numberList = new LinkedList<>();
                numberList.add(numberQuery);
                searchVo.setLkhList(numberList);
            }
        }
    }

    public Long insert(RoutinePatientYz routinePatient) {
        Date now = new Date();
        routinePatient.setCreateTime(now);
        return routinePatientYzMapper.insert(routinePatient) > 0 ? routinePatient.getId() : 0L;
    }

    public int deleteById(Long routinePatientId) {
        return routinePatientYzMapper.deleteById(routinePatientId);
    }

    public Long deleteBySampleId(Long sampleId) {
        return routinePatientYzMapper.deleteBySampleId(sampleId);
    }

    public void updateByLkh( Long sampleId) {
        routinePatientYzMapper.updateByLkh(sampleId);
    }
}
